﻿using LayuiMvc.DataEntity;
using LayuiMvc.DataEntity.IdentityEntity;
using LayuiMvc.DataEntity.IdentityStore;
using LayuiMvc.DataModel.Identity;
using LayuiMvcUtil.Exception;
using LayuiMvcUtil.Result;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LayuiMvc.Logic.Identity
{
    public class IdentityRoleManager:RoleManager<IdentityRoleEntity,int>
    {
        public IdentityRoleManager(IdentityRoleStore store)
            : base(store)
        {

        }

        #region 角色管理==角色列表

        public async Task<DataResult<List<RoleModel>>>  RoleListAsync(int page,int limit)
        {
            DataResult<List<RoleModel>> rtnResult = new DataResult<List<RoleModel>>();

            IQueryable<IdentityRoleEntity> query = base.Roles.OrderBy(m => m.CreateTime);

            rtnResult.count = await query.CountAsync();

            List<RoleModel> roleList = await query.Skip((page - 1) * limit).Take(limit).Select(m => new RoleModel()
            {
                RoleId = m.Id,
                RoleName = m.Name,
                Description = m.Description,
                CreateTime = m.CreateTime,
                UpdateTime = m.UpdateTime,
            }).ToListAsync();

            rtnResult.data = roleList;
            return rtnResult;
        }
        #endregion


        #region 角色管理==添加角色
            
        public async Task RoleAddAsync(IdentityRoleEntity saveEntity)
        {
            saveEntity.CreateTime = DateTime.Now;
            saveEntity.UpdateTime = DateTime.Now;

            IdentityResult result = await base.CreateAsync(saveEntity);

            if (!result.Succeeded)
            {
                throw new CustomException("角色添加失败");
            }
        }
        #endregion

        #region 角色管理==编辑角色

        /// <summary>
        /// 角色编辑初始化
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<IdentityRoleEntity> RoleUpdateInitAsync(int roleId)
        {
            IdentityRoleEntity roleEntity = await base.FindByIdAsync(roleId);

            if (roleEntity==null)
            {
                throw new CustomException("角色数据不存在");
            }

            return roleEntity;
        }

        /// <summary>
        /// 角色编辑
        /// </summary>
        /// <param name="roleId">要修改的角色实体</param>
        /// <returns></returns>
        public async Task RoleUpdateAsync(IdentityRoleEntity saveEntity)
        {
            IdentityRoleEntity roleEntity = await base.FindByIdAsync(saveEntity.Id);

            if (roleEntity==null)
            {
                throw new CustomException("角色数据不存在");
            }
            roleEntity.Name = saveEntity.Name;
            roleEntity.Description = saveEntity.Description;
            roleEntity.UpdateTime = DateTime.Now;

            IdentityResult result = await base.UpdateAsync(roleEntity);

            if (!result.Succeeded)
            {
                throw new CustomException("角色编辑失败");
            }
        }
        #endregion

        #region 角色管理== 删除角色

        public async Task RoleDeleteAsync(int roleId)
        {
            IdentityRoleEntity roleEntity = await base.FindByIdAsync(roleId);

            if (roleEntity==null)
            {
                throw new CustomException("角色数据不存在");
            }

            IdentityResult result = await base.DeleteAsync(roleEntity);

            if (!result.Succeeded)
            {
                throw new CustomException("角色删除失败");
            }
        }
        #endregion


        public static IdentityRoleManager Create(IdentityFactoryOptions<IdentityRoleManager> options,IOwinContext context)
        {
            EFContext db = context.Get<EFContext>();

            IdentityRoleManager roleManager = new IdentityRoleManager(new IdentityRoleStore(db));

            return roleManager;
        }
    }
}
